package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.domian.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.util.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;


    //添加购物车
    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shop) {
        //得到前端传过来的参数
        //判断用户是添加的套餐还是菜品
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(shop.getDishId() != null, ShoppingCart::getDishId, shop.getDishId());
        wrapper.eq(shop.getSetmealId() != null, ShoppingCart::getSetmealId, shop.getSetmealId());
        //得到查询数据库返回的购物车对象
        ShoppingCart shoppingCart = shoppingCartService.getOne(wrapper);
        //判断用户历史购物车有无该类数据
        if (shoppingCart == null) {
            //shoppingCart == null 说明购物车没有用户添加的数据,将用户的数据添加到购物车
            shop.setCreateTime(LocalDateTime.now());
            Long userId = BaseContext.get();
            shop.setUserId(userId);
            //保存用户添加的数据
            shoppingCartService.save(shop);
        } else {
            //用户已经在购物车有该类数据,数量进行修改
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartService.updateById(shoppingCart);
        }
        return R.success("添加成功");
    }


    //查询购物车
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.get());
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }


    //清空购物车
    @DeleteMapping("/clean")
    public R<String> clean() {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.get());
        shoppingCartService.remove(wrapper);
        return R.success("清空购物车成功");
    }

    //减少购物车商品数量
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        wrapper.eq(ShoppingCart::getUserId,BaseContext.get());
        ShoppingCart result = shoppingCartService.getOne(wrapper);
        Integer number = result.getNumber();
        //如果数量大于1,数量减少1
        if (number>1){
            result.setNumber(result.getNumber()-1);
            shoppingCartService.updateById(result);
        }else {
            //数量等于1  删除商品
            shoppingCartService.removeById(result);
        }
        return R.success("修改成功");
    }



}
